<?php
class register_c extends Control
{
	function __construct()
	{
		parent::Control();
		$this->load_model("user");
	}

	function register_c()
	{
		$this->__construct();
	}

	function index_f()
	{
		//判断用户UA是否为微信
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		if (strpos($user_agent, 'MicroMessenger')) {
			$appid='wxa9adc2cde1638e75';
			$secret = "ac5418ff04ffcc3e0bb05c38bff32821"; 
			$ac = $_GET["ac"];
			$rf = $_GET["rf"];
			if(empty($ac)){
				$REDIRECT_URI='http://'.$_SERVER["SERVER_NAME"].'/index.php?c=register&ac=uinfo&rf='.$rf;
				$scope='snsapi_userinfo';//需要授权
				$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
				header("Location:".$url);
				exit();
			}else if($ac=="uinfo"){
				$code = $_GET["code"]; 
				$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
				$ch = curl_init();
				curl_setopt($ch,CURLOPT_URL,$get_token_url);
				curl_setopt($ch,CURLOPT_HEADER,0); 
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
				$res = curl_exec($ch); 
				curl_close($ch); 
				$json_obj = json_decode($res,true);
				//根据openid和access_token查询用户信息 
				$access_token = $json_obj['access_token']; 
				$openid = $json_obj['openid'];
				$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; 
				$ch = curl_init(); 
				curl_setopt($ch,CURLOPT_URL,$get_user_info_url); 
				curl_setopt($ch,CURLOPT_HEADER,0); 
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
				$res = curl_exec($ch); 
				curl_close($ch); 
				//解析json
				$user_obj = json_decode($res,true); 
				if(!empty($user_obj['openid'])){
					$_SESSION["wxinfo"]['openid'] = $user_obj['openid'];
					$_SESSION["wxinfo"]['nickname'] = $user_obj['nickname'];
					$_SESSION["wxinfo"]['headimgurl'] = $user_obj['headimgurl'];
				}
				
				$rs = $this->user_m->user_from_openid($openid);
				if($rs)
				{
					$rf = $_GET["rf"];
					$refer = 'http://'.$_SERVER["SERVER_NAME"].'/';
					if($rf=="customer"){
						$refer = 'http://'.$_SERVER["SERVER_NAME"].'/index.php?c=customer';
					}
					if($rf=="recommend"){
						$refer = 'http://'.$_SERVER["SERVER_NAME"].'/index.php?c=recommend';
					}
					if($rf=="commission"){
						$refer = 'http://'.$_SERVER["SERVER_NAME"].'/index.php?c=commission';
					}
					
					$_SESSION["user_id"] = $rs["id"];
					$_SESSION["user_name"] = $rs["username"];
					$_SESSION["group_id"] = $rs["groupid"];
					$_SESSION["user_rs"]= $rs;
					$_SESSION["headimgurl"] = $rs["headimgurl"];
					header("Location:".$refer);
					exit();
				}
				
				
			}
		}
		
		
		
		if(!$this->sys_config["reg_status"])
		{
			$message = $this->sys_config["close_reg"] ? $this->sys_config["close_reg"] : "No register!";
			error($message,$this->url());
		}

		if($_SESSION["user_id"])
		{
			error('',$this->url('home'));
		}

		$sitetitle = $this->lang["register"];
		$this->tpl->assign("sitetitle",$sitetitle);
		$array[0]["title"] = $this->lang["register"];
		$this->tpl->assign("leader",$array);
		$this->tpl->display("register.".$this->tpl->ext);
	}
	
	//存储个人信息
	function setok_f()
	{
		if(!$this->sys_config["reg_status"])
		{
			$message = $this->sys_config["close_reg"] ? $this->sys_config["close_reg"] : "No register!";
			echo $message;
			exit;
		}
		if($_SESSION["user_id"])
		{
			error('',$this->url('home'));
		}
		$array = array();
		$array["username"] = $this->trans_lib->safe("name");
		$array["phone"] = $this->trans_lib->safe("phone");
		$newpass = $this->trans_lib->safe("password");
		$array["job"] = $this->trans_lib->safe("job");
		$array["company"] = $this->trans_lib->safe("company");
		if(!$newpass)
		{
			echo $this->lang["empty_pass"];
			exit;
		}
		$array["pass"] = sys_md5($newpass);

		$checkphone = $this->checkphone($array["phone"]);
		if($checkphone != "0")
		{
			echo $chkname;
			exit;
		}
		
		$array["regdate"] = $this->system_time;
		$array["status"] = 1;
		//会员组
		$this->load_model("usergroup");
		$group_rs = $this->usergroup_m->get_default();
		$array["groupid"] = $group_rs["id"];
		
		//微信信息
		if(!empty($_SESSION["wxinfo"]['openid'])){
			$array["openid"] = $_SESSION["wxinfo"]['openid'];
			$array["nickname"] = $_SESSION["wxinfo"]['nickname'];
			$array["headimgurl"] = $_SESSION["wxinfo"]['headimgurl'];
		}
		$user_id = $this->user_m->save($array);
		//会员注册成功，模拟登录
		$_SESSION["user_id"] = $user_id;
		$_SESSION["user_name"] = $array["username"];
		$_SESSION["group_id"] = $array["groupid"];
		$tmp_array = $array;
		$tmp_array["id"] = $user_id;
		$_SESSION["user_rs"] = $tmp_array;
		
		//发送欢迎信息
		if($this->sys_config["smtp_reg"])
		{
			$this->load_lib("email");
			$this->email_lib->reg($user_id);
		}
		
		$gtype = $array["groupid"]==2?"中介公司":"大众经纪人";
			
		
		//发送经纪人注册提醒
		include(LIBS.'/WeixinJs/WeixinSMS.class.php');  
		$WeixinSMS = new \WeixinSMS("wxa9adc2cde1638e75","ac5418ff04ffcc3e0bb05c38bff32821");
		$data['first']['value'] = "亲爱的管理员，有经纪人进行了注册！";
		$data['first']['color'] = "#f29700";
		$data['keyword1']['value'] = $array["username"];
		$data['keyword1']['color'] = "#333";
		$data['keyword2']['value'] = $array["phone"];
		$data['keyword2']['color'] = "#333";
		$data['keyword3']['value'] = date("Y-m-d H:i");
		$data['keyword3']['color'] = "#333";
		$data['remark']['value'] = "注册类型：".$gtype;
		$data['remark']['color'] = "#333";
		$WeixinSMS->doSend("ozS1W1KY0VEI8mfynb-AzIBJDq2E","TcU5qLoZ0WPLsK2YpCt16LnKY5nyOuyLzoHB5WniKIQ",'', $data, $topcolor = '#32749a');//李想
		$WeixinSMS->doSend("ozS1W1Cc8yBxHgTCQgHXKe8B3N7w","TcU5qLoZ0WPLsK2YpCt16LnKY5nyOuyLzoHB5WniKIQ",'', $data, $topcolor = '#32749a');//雷云响
		$WeixinSMS->doSend("ozS1W1Pacdg_x_U_OcS2I9Fozw7A","TcU5qLoZ0WPLsK2YpCt16LnKY5nyOuyLzoHB5WniKIQ",'', $data, $topcolor = '#32749a');//黎
		echo "1";
		exit;
	}

	//检测手机号是否存在
	function checkphone_f()
	{
		$phone = $this->trans_lib->safe("phone");
		exit($this->checkphone($phone));
	}

	function checkphone($phone)
	{
		if(!$phone)
		{
			return "1";
		}
		$rs = $this->user_m->user_from_phone($phone);
		if($rs)
		{
			return "1";
		}
		else
		{
			return "0";
		}
	}
}
?>